#ifndef SL_ASYNFRAMEINNERDEF_H_20220123_
#define SL_ASYNFRAMEINNERDEF_H_20220123_

//实现多线程，会话机制，事件机制
#include <map>
#include <mutex>
#include <stdint.h>
#include <memory>
#include <atomic>
#include "SLT_timer.h"
#include "SLT_thread_pool.h"
#include "SL_asynFrame.h"

enum SLF_event_type
{
	SLF_event_start,  //开始
	SLF_event_stop,   //停止
	SLF_event_data,   //普通数据
	SLF_event_timer   //定时器
};
//事件信息
struct SLF_event
{
	virtual ~SLF_event() {}
	SLF_event_type m_type;
};

struct SLF_event_memData:public SLF_event
{
	SLF_sendMemBasePtr m_data;
	uint32_t  m_srcSid = 0; //发送方的session
	SLF_senderPtr  m_sender;
	int       m_msgid = 0;
};

class SLF_frameImp;
//frame 内部调度会话对象
class SLF_SessionTask : public SLF_SessionTaskBase
{
	struct timer_overlapped : public SLF_event
	{
		uint32_t timerID = 0;
		int intervalMs = 0;
		bool periodic = false;

		uint32_t sid = 0;
		SLT_TIMER_HANDLE th = SLT_TIMER_HANDLE_INVLAID;

		SLT_timer_CallBackType timerCBType = SLT_timerDue;
	};
	enum sessionStateEnum
	{
		sessionState_init,
		sessionState_start,
		sessionState_stoping,  //调用了删除接口，还没有真正完成.停止后不在处理任何事件。后面的事件全部丢弃
		sessionState_stop     //停止后不在处理任何事件。后面的事件全部丢弃
	};
public:
	~SLF_SessionTask();
	virtual void setTimer(uint32_t timerID, int _intervalMs, bool periodic);
	virtual void delSelf();
	virtual uint32_t getSessID() { return m_sessid; }

	static void     onTimerCB(SLT_timer_CallBackType _type, SLT_TIMER_HANDLE timerid, void* userData);
	bool            postMsg(uint32_t _SessID, int msgid, SLF_sendMemBasePtr _data);

	void pushEvent(SLF_event *_pev);
	void onEvent();
public:
	sessionStateEnum   m_state = sessionState_init;

	std::map<uint32_t, timer_overlapped*>  m_timerMap;  //key为timerID

	std::mutex  m_eventListLock; 
	std::deque<SLF_event *>   m_eventList;  //消息队列
	bool                      m_run = false;        //表示是否有线程在执行

	uint32_t m_sessid = 0;

	SLF_SessionBasePtr m_pSession;  //托管的业务session对象
	SLF_frameImp  *m_owerFrame;   //自身所属框架
};
//typedef std::shared_ptr<SLF_SessionTask> SLF_SessionTaskPtr;
typedef SLT_EmbedSmartPtr<SLF_SessionTask> SLF_SessionTaskPtr;

class SLF_frameImp : public SLF_frame
{
	class frameThreadTask :public slt_thread_task
	{
	public:
		virtual void onRun();
		uint32_t m_sessID = 0;
	};
public:
	virtual int SLF_init(int _threadNum);
	//uint32_t SLF_uninit(int _threadNum);

	virtual int SLF_start() { return 0; }
	//uint32_t SLF_stop();

	virtual int SLF_addSession(const SLF_SessionBasePtr &_psession);
	virtual void SLF_delSession(uint32_t _sessId);
	virtual bool SLF_postMsg(uint32_t _SessID, int msgid, uint32_t _srcID,const SLF_sendMemBasePtr &_data);
	bool	 SLF_postMsg2(uint32_t _SessID, int msgid, uint32_t _srcID, SLF_sendMemBasePtr &_data,const SLF_senderPtr& _senderPtr);
	bool     SLF_pushEvent(uint32_t _SessID, int msgid, uint32_t _srcID, SLF_sendMemBasePtr &_data,const SLF_senderPtr& _senderPtr);
public:
	void  postTask(uint32_t _sessID);
public:
	slt_thread_pool   m_threadPool;
	
	//下面是全局对象
public:
	static void  SLF_global_init();
	static SLF_SessionTaskPtr findSession(uint32_t _sid);
	static void delSession(uint32_t _sid);
	static uint32_t   getSessionID();
	//static int postEvent(uint32_t _SessID, SLF_event* _ev);
	
public:
	static std::mutex   g_globalInitLock;
	static bool         g_bInit;
	static std::atomic<uint32_t>   g_sessionIDBase;

	//static std::mutex  g_sessionMapLock_20220124;
	static std::map<uint32_t, SLF_SessionTaskPtr>  g_sessionMap;
};



#endif